
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;

namespace UserManagement.Gui
{
	public class UserSignUpBizProc : UserManagementBizProc
	{
		public UserSignUpBizProc(ActiveBizProc procState):base(procState) { }

		protected override void setupRuleNames()
		{
			base.setupRuleNames();
			m_updateBizObjRule = "setupUserProfile";
		}

		public override void addNewBizObj()
		{
			if (!UserLoggedIn)
			{
				base.addNewBizObj();
				updateBizObj();
			}
		}

		public override IBizObj createNewBizObj()
		{
			IBizObj user = App.bizObjMgr.create("User");
			user.put("companyId" ,ActiveCompanyId );
			user.put("id", new Swaf.BGuid.Guid());
			return user;
		}
		public override void updateActionState()
		{
			base.updateActionState();
			((GenericAction)Actions["AddBizObj"]).Availability = ActionAvailability.enabled;

		}
		public override IResults updateBizObj(IBizObj obj)
		{
			if (obj != null)
			{
				IResults rc = App.ruleMgr.execute("isUserNameUnique", obj.get("userName").ToString());
				{
					if (rc.ExceptionInfo == null && (bool)rc.CallResults)
					{
						rc = base.updateBizObj(obj);
						if (rc.ExceptionInfo == null)
						{
							rc = App.ruleMgr.execute("loginUser", obj);
							if (rc.ExceptionInfo != null)
								displayMessageFromBizRuleException("Error-UpdateUserFailed", rc.ExceptionInfo);
								
						}
						else
							displayMessageFromBizRuleException("Error-UpdateUserFailed", rc.ExceptionInfo);
						return rc;
					}
				}
			}
			return null;
		}
		public override Swaf.Security.Privileges CurrentEditPrivilege{get { return !UserLoggedIn ? Privileges.Delete : Privileges.ReadOnly; }}
		
		public bool UserLoggedIn{get { return (bool)CurrentUser.get("authenticated"); }}
	}
}
